<script setup>
import {Message} from '@arco-design/web-vue';
import axios from "axios";
import {onMounted, ref, toRefs, watch} from "vue";
import processBusData from "@/components/processBusData.js";
let oldDate='';
onMounted(()=>{
  oldDate='';
  // queryList(processBusData.getDateString(new Date()));
})
const props=defineProps({
  conf: Object
})
const {conf}=toRefs(props)
watch(()=>conf,(newV,oldV)=>{

  console.log(conf.value.date)
  //if different date str

    if(newV.value.date!==oldDate){
      queryList(newV.value.date);
    }else{
      resetList();
    }

},{immediate:true,deep:true})

const data = ref([])
const stuData = ref([]);
const columns = [
  {
    title: '类型',
    dataIndex: 'type',
  },
  {
    title: '出发时间',
    dataIndex: 'time',
    width: 200,
    sortable: {
      sortDirections: ['ascend', 'descend']
    }
  },
  {
    title: '始发站',
    dataIndex: 'start',
  },
  {
    title: '终点站',
    dataIndex: 'end',
  },
  {
    title: '票价',
    dataIndex: 'price',
  },
  {
    title: '余票数',
    dataIndex: 'remainTickets',
  },
];

let connected=true;
function resetList() {
  data.value=[];
  let temp=[];

  //get no remain seat
  stuData.value.forEach((v, i) => {
    if(v.ramainTickets===0){
      if(conf.value.showNoRemain){
        temp.push(v);
      }
    }else{
      temp.push(v);
    }
  })

  console.log(conf.value.route)
  //get selected route
  if(conf.value.route!==0){
    let temp1=[];
    temp.forEach((v,i)=>{
      if(v.pathId===conf.value.route){
        temp1.push(v);
      }
    })
    temp=temp1;
  }



  temp.forEach((v,i)=>{
    data.value.push(processBusData.raw2Stu(v));
  })
  console.log(temp)
}


function queryList(day) {

  oldDate=day;
  return axios.post('/vcard/shu-wechat-client/schoolbus/passenger/queryDayBusRouteList', {
    departureTime: day,
    saleStatus: 1
  }, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
      .then(response => {
        if(response.data.dataList.length===0){
          queryReserveList(day);
        }else{
          if (!connected) {
            connected = true;
            Message.success('连接成功！校车列表已刷新...')
          }
          stuData.value = response.data.dataList;
          console.log("请求到数据：")
          console.log(stuData.value)
          resetList()
        }
      }, error => {
        if (connected) {
          const hours = new Date().getHours();
          connected = false;
          if (hours >= 23 || hours <= 5) Message.error('刷新失败！学校服务器关闭,请在工作时间再试...');
          Message.error('刷新失败！请检查网络连接。');
        }
      })
}
const scrollPercent = {
  x: '120%',
  y: '100%'
};
function queryReserveList(day) {
  oldDate=day;
  return axios.post('/vcard/shu-wechat-client/schoolbus/passenger/queryDayBusRouteList', {
    departureTime: day,
    saleStatus: 0
  }, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
      .then(response => {
        if (!connected) {
          connected = true;
          Message.success('连接成功！校车列表已刷新...')
        }
        stuData.value = response.data.dataList;
        console.log("请求到数据：")
        console.log(stuData.value)
        resetList()
      }, error => {
        if (connected) {
          const hours = new Date().getHours();
          connected = false;
          if (hours >= 23 || hours <= 5) Message.error('刷新失败！学校服务器关闭,请在工作时间再试...');
          Message.error('刷新失败！请检查网络连接。');
        }
      })
}
</script>

<template>
  <div class="list">
    <a-row class="grid-demo">
      <a-col :span="4">

      </a-col>
      <a-col :span="16">
        <a-table :data="data" :columns="columns" sticky-header="true"
                 scroll="true" :pagination="false" :scroll="scrollPercent"
                 :hoverable="true" style="height: 600px" column-resizable size="large">

        </a-table>
      </a-col>

      <a-col :span="4">

      </a-col>
    </a-row>

  </div>

</template>

<style scoped>
.list{
  margin-top: 20px;

}
</style>